<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/11/25 0025
 * Time: 9:19
 */
namespace app\adminapi\controller;

use app\adminapi\model\Api;
use app\api\model\JWT;
use think\App;
use think\Controller;
use think\Db;

/**
 * @title 后台订单
 * @description 接口说明
 */
class Order extends Api
{
    /**
     * @title 零点订单
     * @description 接口说明
     * @author 开发者
     * @url /adminapi/order/zero
     * @method POST
     * @param name:page type:int require:1 default: other: desc:页码
     * @param name:name type:int require:0 default: other: desc:搜索姓名或者手机号
     * @param name:again_time type:int require:0 default: other: desc:时间段开始时间
     * @param name:end_time type:int require:0 default: other: desc:时间段结束时间
     * @param name:director type:int require:0 default: other: desc:客户经理id
     * @param name:type type:int require:0 default: other: desc:1预定日期2宴会日期
     * @param name:is_type type:int require:0 default: other: desc:1退订2入座
     * 
     * @return list:列表@
     * @list id:id addtime:预定时间 yu_name:预订人 mobile:电话 yu_times:预定日期 status:1预留2入座3退订 elegant:雅间 people:人数 director:客户经理
     * @return page:总页数
     */
    public function zero()
    {
        $user = $this->user;
        $page = input('page')??1;
        $type = input('type')??1;
        $name = input('name');
        $again_time = input('again_time');
        $end_time = input('end_time');
        $director = input('director');
        $is_type = input('is_type');
        // 手机号与昵称
        if(is_numeric($name)){
            $where[] = ['mobile','like',"%".$name."%"];
        }else{
            $where[] = ['name','like',"%".$name."%"];
        }
        // 客户经理
        if($director){
            $where[] = ['director_id','=',$director];
        }
        // 订单类型
        if($is_type == 1){
            $where[] = ['status','=',3];
        }elseif($is_type == 2){
            $where[] = ['status','=',2];
        }
        $where[] = ['hid','=',$user['hid']];
        // 排序
        if($type == 1){
            if($again_time && $end_time){
                $again_time = strtotime($again_time);
                $end_time = strtotime($end_time);
            $where[] = ['addtime','between',[$again_time,$end_time]];
            }else{
                //当天开始时间
                $again_time=strtotime(date("Y-m-d",time()));
                //当天结束之间
                $end_time=$again_time+60*60*24;
            }
            $list = db::name('zero')->where($where)->field('id,people,sex,yu_name,mobile,name,eid,status,director,time,addtime,year,month,day')->order('addtime desc')->page("$page,10")->select();
            foreach ($list as $k => $v) {
                $list[$k]['yu_times'] = $v['year'] . '-' . $v['month'] . '-' . $v['day'] . ' ' .$v['time'];
                $list[$k]['yu_name'] = $v['name'];
            }
            $lists = array_column($list, 'addtime');
            array_multisort($lists, SORT_DESC, $list);
        }elseif($type == 2){
            $list = db::name('zero')->where($where)->field('id,people,sex,yu_name,mobile,eid,status,director,time,addtime,year,month,day,name')->page("$page,10")->select();
            foreach ($list as $k => $v) {
                $list[$k]['yu_times'] = $v['year'] . '-' . $v['month'] . '-' . $v['day'] . ' ' .$v['time'];
                $list[$k]['yu_name'] =  $v['name'];
            }
            $lists = array_column($list, 'yu_times');
            array_multisort($lists, SORT_DESC, $list);
        }
        
        if($list){
            foreach ($list as $ks => $vs) {
                $list[$ks]['addtime'] =  date('Y-m-d H:i:s',$vs['addtime']);
                $elegant = db::name('elegant')->where('id',$vs['eid'])->field('id,name')->find();
                    if($elegant){
                        $list[$ks]['elegant'] = urldecode($elegant['name']);
                    }
                    unset($list[$ks]['year']);
                    unset($list[$ks]['month']);
                    unset($list[$ks]['day']);
                    unset($list[$ks]['time']);
                    unset($list[$ks]['eid']);
            }
        }
        $total = db::name('zero')->where($where)->count();
        $data['list'] = $list;
        $data['page'] = ceil($total/10);
        $data['total'] = $total;
        $data['pages'] = 10;
        return $this->ajaxReturn(0,'成功',$data);
    }
    /**
     * @title 宴会订单
     * @description 接口说明
     * @author 开发者
     * @url /adminapi/order/banquet
     * @method POST
     * @param name:page type:int require:1 default: other: desc:页码
     * @param name:name type:int require:0 default: other: desc:搜索姓名或者手机号
     * @param name:again_time type:int require:0 default: other: desc:时间段开始时间
     * @param name:end_time type:int require:0 default: other: desc:时间段结束时间
     * @param name:director type:int require:0 default: other: desc:客户经理id
     * @param name:type type:int require:0 default: other: desc:1预定日期2宴会日期
     * @param name:is_type type:int require:0 default: other: desc:1预定订单2潜客订单3所有
     * 
     * @return list:列表@
     * @list id:id addtime:预定时间 book:预订人 mobile:电话 t_name:预定类型 yu_times:宴会日期 b_name:宴会厅 desk:桌数 prepare_desk:备桌 director:客户经理 scheduled_deposit:押金
     * @return page:总页数
     */
    public function banquet()
    {
        $user = $this->user;
        $page = input('page')??1;
        $type = input('type')??1;
        $name = input('name');
        $again_time = input('again_time');
        $end_time = input('end_time');
        $director = input('director');
        $is_type = input('is_type');
        // 手机号与昵称
        if(is_numeric($name)){
            $where[] = ['mobile','like',"%".$name."%"];
        }else{
            $where[] = ['book','like',"%".$name."%"];
        }
        // 客户经理
        if($director){
            $where[] = ['director_id','=',$director];
        }
        // 订单类型
        if($is_type == 1){
            $where[] = ['type','=',2];
        }elseif($is_type == 2){
            $where[] = ['type','=',1];
        }
        $where[] = ['hid','=',$user['hid']];
        $where[] = ['is_meeting','=',2];
        // 排序
        if($type == 1){
            if($again_time && $end_time){
                $again_time = strtotime($again_time);
                $end_time = strtotime($end_time);
                $where[] = ['addtime','between',[$again_time,$end_time]];
            }else{
                //当天开始时间
                $again_time=strtotime(date("Y-m-d",time()));
                //当天结束之间
                $end_time=$again_time+60*60*24;
            }
            // 
            $list = db::name('scheduled')->where($where)->field('id,addtime,sex,book,mobile,t_name,type,begin_year,begin_month,begin_day,begin_night,b_name,desk,prepare_desk,director')->order('addtime desc')->page("$page,10")->select();
        }elseif($type == 2){
            $list = db::name('scheduled')->where($where)->field('id,addtime,sex,book,mobile,t_name,type,begin_year,begin_month,begin_day,begin_night,b_name,desk,prepare_desk,director')->page("$page,10")->select();
            foreach ($list as $k => $v) {
                $list[$k]['yu_times'] = $v['begin_year'] . '-' . $v['begin_month'] . '-' . $v['begin_day'];
                $list[$k]['yu_time'] = strtotime($list[$k]['yu_times']);
            }
            $lists = array_column($list, 'yu_time');
            array_multisort($lists, SORT_DESC, $list);
        }
        if($list){
            foreach ($list as $ks => $vs) {
                $list[$ks]['addtime'] =  date('Y-m-d H:i:s',$vs['addtime']);
                if($vs['begin_night'] == 1){
                    $begin_night = '中午';
                }else{
                    $begin_night = '晚上';
                }
                $list[$ks]['yu_times'] = $vs['begin_year'] . '-' . $vs['begin_month'] . '-' . $vs['begin_day'] . ' ' . $begin_night;
                $list[$ks]['scheduled_deposit'] = db::name('scheduled_deposit')->where('sid',$vs['id'])->sum('money');
                unset($list[$ks]['begin_year']);
                unset($list[$ks]['begin_month']);
                unset($list[$ks]['begin_day']);
            }
        }
        $total = db::name('scheduled')->where($where)->count();
        $data['list'] = $list;
        $data['page'] = ceil($total/10);
        $data['pages'] = 10;
        $data['total'] = $total;
        return $this->ajaxReturn(0,'成功',$data);
    }
    /**
     * @title 会议订单
     * @description 接口说明
     * @author 开发者
     * @url /adminapi/order/banquet_meeting
     * @method POST
     * @param name:page type:int require:1 default: other: desc:页码
     * @param name:name type:int require:0 default: other: desc:搜索姓名或者手机号
     * @param name:again_time type:int require:0 default: other: desc:时间段开始时间
     * @param name:end_time type:int require:0 default: other: desc:时间段结束时间
     * @param name:director type:int require:0 default: other: desc:客户经理id
     * @param name:type type:int require:0 default: other: desc:1预定日期2宴会日期
     * @param name:is_type type:int require:0 default: other: desc:1预定订单2潜客订单3所有
     * 
     * @return list:列表@
     * @list id:id addtime:预定时间 book:预订人 mobile:电话 t_name:预定类型 begin_time:开始日期 end_time:结束日期 b_name:宴会厅 desk:桌数 prepare_desk:备桌 director:客户经理 scheduled_deposit:押金
     * @return page:总页数
     */
    public function banquet_meeting()
    {
        $user = $this->user;
        $page = input('page')??1;
        $type = input('type')??1;
        $name = input('name');
        $again_time = input('again_time');
        $end_time = input('end_time');
        $director = input('director');
        $is_type = input('is_type');
        // 手机号与昵称
        if(is_numeric($name)){
            $where[] = ['mobile','like',"%".$name."%"];
        }else{
            $where[] = ['book','like',"%".$name."%"];
        }
        // 客户经理
        if($director){
            $where[] = ['director_id','=',$director];
        }
        // 订单类型
        if($is_type == 1){
            $where[] = ['type','=',2];
        }elseif($is_type == 2){
            $where[] = ['type','=',1];
        }
        $where[] = ['hid','=',$user['hid']];
        $where[] = ['is_meeting','=',1];
        // 排序
        if($type == 1){
            if($again_time && $end_time){
                $again_time = strtotime($again_time);
                $end_time = strtotime($end_time);
            $where[] = ['addtime','between',[$again_time,$end_time]];

            }else{
                //当天开始时间
                $again_time=strtotime(date("Y-m-d",time()));
                //当天结束之间
                $end_time=$again_time+60*60*24;
            }
            $list = db::name('scheduled')->where($where)->field('id,sex,addtime,book,mobile,t_name,type,begin_year,begin_month,begin_day,begin_night,end_year,end_month,end_day,end_night,b_name,desk,prepare_desk,director')->order('addtime desc')->page("$page,10")->select();
        }elseif($type == 2){
            $list = db::name('scheduled')->where($where)->
            field('id,sex,addtime,book,mobile,t_name,type,begin_year,begin_month,begin_day,begin_night,end_year,end_month,end_day,end_night,b_name,desk,prepare_desk,director')->page("$page,10")->select();
            foreach ($list as $k => $v) {
                $list[$k]['yu_times'] = $v['begin_year'] . '-' . $v['begin_month'] . '-' . $v['begin_day'];
                $list[$k]['yu_time'] = strtotime($list[$k]['yu_times']);
            }
            $lists = array_column($list, 'yu_time');
            array_multisort($lists, SORT_DESC, $list);
        }
        if($list){
            foreach ($list as $ks => $vs) {
                $list[$ks]['addtime'] =  date('Y-m-d H:i:s',$vs['addtime']);
                if($vs['begin_night'] == 1){
                    $begin_night = '中午';
                }else{
                    $begin_night = '晚上';
                }
                if($vs['end_night'] == 1){
                    $end_night = '中午';
                }else{
                    $end_night = '晚上';
                }
                $list[$ks]['begin_time'] = $vs['begin_year'] . '-' . $vs['begin_month'] . '-' . $vs['begin_day'] . ' ' . $begin_night;
                $list[$ks]['end_time'] = $vs['end_year'] . '-' . $vs['end_month'] . '-' . $vs['end_day'] . ' ' . $end_night;
                $list[$ks]['scheduled_deposit'] = db::name('scheduled_deposit')->where('sid',$vs['id'])->sum('money');
                unset($list[$ks]['begin_year']);
                unset($list[$ks]['begin_month']);
                unset($list[$ks]['begin_day']);
            }
        }
        $total = db::name('scheduled')->where($where)->count();
        $data['list'] = $list;
        $data['page'] = ceil($total/10);
        $data['total'] = $total;
        $data['pages'] = 10;
        return $this->ajaxReturn(0,'成功',$data);
    }
    /**
    * @title 宴会订单导出（未补充）
    * @description 接口说明
    * @author 开发者
    * @url /adminapi/order/banquet_export
    * @method POST
    * @param name:name type:int require:0 default: other: desc:搜索姓名或者手机号
    * @param name:again_time type:int require:0 default: other: desc:时间段开始时间
    * @param name:end_time type:int require:0 default: other: desc:时间段结束时间
    * @param name:director type:int require:0 default: other: desc:客户经理id
    * @param name:type type:int require:0 default: other: desc:1预定日期2宴会日期
    * @param name:is_type type:int require:0 default: other: desc:1预定订单2潜客订单3所有
    */
    public function banquet_export()
    {
        // 查询数据
        $data = db::name('elegant')->select();
        // 设置表头信息（对应字段名,宽度，显示表头名称）
        $cellName = [
            ['id', 'auto', 'ID'],
            ['name', 'auto', '级别名称'],
        ];
        // 调用插件（传入插件名，[导出文件名、表头信息、具体数据]）
        plugin_action('Excel/Excel/export', ['test', $cellName, $data]);
    }
    /**
     * @title 获取客户经理
     * @description 接口说明
     * @author 开发者
     * @url /adminapi/order/director
     * @method POST
     * 
     * @return list:列表@
     * @list id:id username:姓名 
     * @return page:总页数
     */
    public function director()
    {
        $user = $this->user;
        $where[] = ['hid','=',$user['hid']];
        $users = db::name('user')->where($where)->field('id,username')->select();
        $data['list'] = $users;
        return $this->ajaxReturn(0,'成功',$data);
    }
    /**
     * @title 宴会订单详情
     * @description 接口说明
     * @author 开发者
     * @url /adminapi/order/banquet_detail
     * @method POST
     * @param name:id type:int require:1 default: other: desc:订单id
     * 
     * @return list:列表@
     * @list id:id d_money:餐标 increase_money:增值金额 source:客户来源 contract:上传合同 other:其他信息 content:备注
     * @return deposit:列表@!
     * @deposit num:订单号 money:金额 pay_name:支付方式 type:1预付款2担保人 name:担保人姓名
     */
    public function banquet_detail()
    {
        $user = $this->user;
        $id = input('id');
        if(!$id){
            return $this->ajaxReturn(1,'缺少参数');
        }
        $where[] = ['id','=',$id];
        $list = db::name('scheduled')->where($where)->field('id,d_money,d_name,increase_money,source,contract,other,content')->find();
        $deposit = db::name('scheduled_deposit')->where('status',2)->where('sid',$list['id'])->field('num,money,addtime,pay_name')->select();
        $guarantee = db::name('guarantee_order')->where('status',2)->where('did',$list['id'])->field('gid,addtime')->select();
        if($deposit){
            foreach($deposit as $dk => $dv){
                $deposit[$dk]['type'] = 1;
            }
        }
        if($guarantee){
            foreach($guarantee as $gk => $gv){
                if($gv['gid']){
                    $guarantee[$gk]['name'] = db('guarantee')->where('id',$gv['gid'])->value('name');
                }
                $guarantee[$gk]['type'] = 2;
            }
        }
        $newArr = array_merge($deposit,$guarantee);
        if($list['contract']){
            $image = explode( ",",$list['contract']);
            if(count($image) != 1){
                $pic = array();
                foreach($image as $vs){
                    $pic[] = get_file_path($vs);
                }
                $list['contract'] = $pic;
            }else{
                $pics = array();
                $pics[] = get_file_path($list['contract']);
                $list['contract'] = $pics;
            }
        }
        if($list['other']){
            $images = explode( ",",$list['other']);
            if(count($images) != 1){
                $pic = array();
                foreach($images as $vs){
                    $pic[] = get_file_path($vs);
                }
                $list['other'] = $pic;
            }else{
                $pics = array();
                $pics[] = get_file_path($list['other']);
                $list['other'] = $pics;
            }
        }
        $source = db::name('source')->where('id',$list['source'])->field('name,id')->find();
        if($user){
            $list['source'] = urldecode($source['name']);
        }

        $a = array();
        foreach($newArr as $key=>$val){
        $a[] = $val['addtime'];//这里要注意$val['nums']不能为空，不然后面会出问题
        }
        //$a先排序
        rsort($a);
        $a = array_flip($a);
        $result = array();
        foreach($newArr as $k=>$v){
        $temp1 = $v['addtime'];
        $temp2 = $a[$temp1];
        $result[$temp2] = $v;
        }
        //这里还要把$result进行排序，健的位置不对
        ksort($result);
        $list['deposit'] = $result;
        $data['list'] = $list;
        return $this->ajaxReturn(0,'成功',$data);
    }
}